/*
 *  Copyright (c) 2010-2023
 */
package net.siufung.market.base.uuid.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.base.Strings;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import net.siufung.core.result.Result;
import net.siufung.core.result.ResultFail;
import net.siufung.core.result.ResultOk;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import net.siufung.core.model.enums.DeletedEnum;
import net.siufung.core.page.PagerResult;
import net.siufung.core.utils.CopyUtil;
import net.siufung.market.base.uuid.service.ISysUuidRulesService;
import net.siufung.market.base.uuid.mapper.ISysUuidRulesMapper;
import net.siufung.market.base.uuid.controller.model.SysUuidRulesModel;
import net.siufung.market.base.uuid.mapper.entity.SysUuidRules;
import net.siufung.market.base.uuid.controller.assembler.SysUuidRulesAssembler;
import net.siufung.market.base.uuid.controller.regulation.SysUuidRulesRegulation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import lombok.AllArgsConstructor;

/**
 * 流水号配置表 前端控制层
 *
 * @author 陈建峰
 * @since 2023-10-09 22:16:17
 * @version 1.0.0
 */
@Api(tags = "流水号配置表")
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/sys/uuid/rules")
public class SysUuidRulesController {

    private final ISysUuidRulesService sysUuidRulesService;
    private final ISysUuidRulesMapper sysUuidRulesMapper;

    /**
     * 通过主键获取 流水号配置表
     * @param id 主键
     * @return Result<SysUuidRulesModel.Vo>
     * */
    @ApiOperation(value = "通过Id获取流水号配置表")
    @GetMapping(value = "/id/{id}")
    public Result<SysUuidRulesModel.Vo> getById(
            @PathVariable("id") Integer id) {
        SysUuidRules sysUuidRules = sysUuidRulesMapper.selectById(id);
        if(Objects.nonNull(sysUuidRules)){
            return Result.ok(SysUuidRulesAssembler.from(sysUuidRules));
        }
        return Result.fail();
    }

    /**
     * 通过主键删除 流水号配置表 对象
     * @param id 主键
     * @return Result<Void>
     * */
    @ApiOperation(value = "通过主键删除流水号配置表对象")
    @PostMapping(value="/remove/{id}")
    public Result<Void> removeById(
            @PathVariable("id") Integer id) {
        SysUuidRulesRegulation.Remove sysUuidRulesRegulationRemove = new SysUuidRulesRegulation.Remove(sysUuidRulesMapper);
        sysUuidRulesRegulationRemove.isSatisfiedBy(id);
        if(sysUuidRulesService.removeById(id)){
            return Result.ok(ResultOk.REMOVE);
        }
        return Result.fail(ResultFail.DATA_REMOVE_ERROR);
    }

    /**
     * 通过主键集合批量删除 流水号配置表
     * @param ids 主键集合
     * @return Result<Void>
     * */
    @ApiOperation(value = "通过主键集合批量删除流水号配置表对象")
    @PostMapping(value="/removeByIds")
    public Result<Void> removeByIds(
            @RequestBody Integer[] ids) {
        if(sysUuidRulesService.removeByIds(Arrays.asList(ids))){
            return Result.ok(ResultOk.REMOVE);
        }
        return Result.fail(ResultFail.DATA_REMOVE_ERROR);
    }

    /**
     * 通过主键更新 流水号配置表 信息
     * @param sysUuidRulesUpdateDto 对象属性
     * @return Result<Void>
     * */
    @ApiOperation(value = "更新 流水号配置表 信息")
    @PostMapping(value="/updateById")
    public Result<Void> updateById(
            @RequestBody SysUuidRulesModel.UpdateDto sysUuidRulesUpdateDto) {
        sysUuidRulesUpdateDto.validated();
        SysUuidRulesRegulation.Update sysUuidRulesRegulationUpdate = new SysUuidRulesRegulation.Update(sysUuidRulesMapper);
        sysUuidRulesRegulationUpdate.isSatisfiedBy(sysUuidRulesUpdateDto);
        SysUuidRules sysUuidRulesOf = sysUuidRulesMapper.selectById(sysUuidRulesUpdateDto.getId());
        CopyUtil.copyPropertiesIgnoreNull(sysUuidRulesUpdateDto.to(), sysUuidRulesOf);
        if(sysUuidRulesService.updateById(sysUuidRulesOf)){
            return Result.ok(ResultOk.UPDATE);
        }
        return Result.fail(ResultFail.DATA_UPDATE_ERROR);
    }

    /**
     * 更新 流水号配置表 状态
     * @param id 主键
     * @param deleted 数据状态
     * @return Result<Void>
     * */
    @ApiOperation(value = "更新 流水号配置表 状态")
    @PostMapping(value="/status/{id}/{deleted}")
    public Result<Void> updateStatusById(
            @PathVariable("id") Integer id,
            @PathVariable("deleted") boolean deleted) {
        SysUuidRulesRegulation.Status sysUuidRulesRegulationStatus = new SysUuidRulesRegulation.Status(sysUuidRulesMapper);
        sysUuidRulesRegulationStatus.isSatisfiedBy(id);
        if(sysUuidRulesService.updateStatusById(id, DeletedEnum.get(deleted))){
            return Result.ok(ResultOk.STATUS);
        }
        return Result.fail(ResultFail.DATA_STATUS_ERROR);
    }

    /**
     * 获取 流水号配置表 分页
     * @param sysUuidRulesPageDto 属性
     * @return Result<IPager<SysUuidRulesModel.Vo>>
     * */
    @ApiOperation(value = "获取 流水号配置表 分页")
    @PostMapping(value="/page")
    public Result<PagerResult<SysUuidRulesModel.Vo>> getPage(
            @RequestBody SysUuidRulesModel.PageDto sysUuidRulesPageDto) {
        sysUuidRulesPageDto.validated();
        QueryWrapper<SysUuidRules> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByDesc(SysUuidRules::getId);
        wrapper.lambda().eq(Objects.nonNull(sysUuidRulesPageDto.getUuidId()),
                SysUuidRules::getUuidId, sysUuidRulesPageDto.getUuidId());
        wrapper.lambda().eq(!Strings.isNullOrEmpty(sysUuidRulesPageDto.getUuidPartType()),
                SysUuidRules::getUuidPartType, sysUuidRulesPageDto.getUuidPartType());
        IPage<SysUuidRules> sysUuidRulesPage = sysUuidRulesMapper.selectPage(
                new Page<>(sysUuidRulesPageDto.getPageNo(), sysUuidRulesPageDto.getPageSize()), wrapper);
        List<SysUuidRules> sysUuidRulesList = sysUuidRulesPage.getRecords();
        PagerResult<SysUuidRulesModel.Vo> sysUuidRulesVoPage = new PagerResult<>(
                sysUuidRulesPage.getCurrent(), sysUuidRulesPage.getSize(), sysUuidRulesPage.getTotal());
        sysUuidRulesVoPage.setRecords(
                SysUuidRulesAssembler.fromList(sysUuidRulesList));
        return Result.ok(sysUuidRulesVoPage);
    }

    /**
     * 获取 流水号配置表 列表
     * @param sysUuidRulesDto 属性
     * @return Result<List<SysUuidRulesModel.Vo>>
     * */
    @ApiOperation(value = "获取流水号配置表列表")
    @PostMapping(value = "/list")
    public Result<List<SysUuidRulesModel.Vo>> list(
            @RequestBody SysUuidRulesModel.Dto sysUuidRulesDto) {
        List<SysUuidRules> sysUuidRulesList = sysUuidRulesMapper.selectList(Wrappers.query(sysUuidRulesDto.to()));
        return Result.ok(SysUuidRulesAssembler.fromList(sysUuidRulesList));
    }

    /**
     * 创建 流水号配置表
     * @param sysUuidRulesSaveDto 对象属性
     * @return Result<Void>
     * */
    @ApiOperation(value = "创建 流水号配置表")
    @PostMapping(value="/save")
    public Result<Void> save(
            @RequestBody SysUuidRulesModel.SaveDto sysUuidRulesSaveDto) {
        sysUuidRulesSaveDto.validated();
        SysUuidRulesRegulation.Save sysUuidRulesRegulationSave = new SysUuidRulesRegulation.Save(sysUuidRulesMapper);
        sysUuidRulesRegulationSave.isSatisfiedBy(sysUuidRulesSaveDto);
        if(sysUuidRulesService.save(sysUuidRulesSaveDto.to())){
            return Result.ok(ResultOk.ADD);
        }
        return Result.fail(ResultFail.DATA_ADD_ERROR);
    }

}